#include "UDPSocket.h"
#include "SocketAddress.h"


namespace CustomSocket
{
	UDPSocket::UDPSocket()
	{
		this->mSocket = socket(AF_INET, SOCK_DGRAM, 0);
	}

	UDPSocket::~UDPSocket()
	{
		closesocket(this->mSocket);
	}

	int UDPSocket::Bind(SocketAddress& socketAddressPrt, USHORT len)
	{
		auto addr_prt = socketAddressPrt.GetSockAddr();
		auto err = bind(this->mSocket, addr_prt, socketAddressPrt.GetSize());
		return err;
	}

	int UDPSocket::SendTo(SocketAddress& targetAddressPrt, const void* buf, USHORT bufLen)
	{
		return send(this->mSocket, static_cast<const char*>(buf), bufLen, 0);
	}

	int UDPSocket::Receive(char* buf, USHORT maxBufLen, sockaddr* fromSockAddr, int* formSockAddrLen)
	{
		return recvfrom(this->mSocket, buf, maxBufLen, 0, fromSockAddr, formSockAddrLen);
	}
}